//
//  DatePickerViewController.m
//  QRMaster
//
//  Created by myApple on 15/7/21.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "DatePickerViewController.h"
#import "SetDateTimeCell.h"
#import "setTimeModel.h"
#import "weekSelectController.h"
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "Reachability.h"
#import "QRSingleton.h"

@interface DatePickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,UITableViewDataSource,UITableViewDelegate,setDateTimeCellDelegate>
{

    UITableView * timeTableView;
    UIView * tableViewPromtView;//tableView的遮罩层
    NSMutableArray * timeDataArray;
    NSArray * weakData;
    
    NSMutableArray * hourArray;
    NSMutableArray * miunitArray;
    
    UIView * timeView;//时间周期
    NSString * beginHourString;
    NSString * beginMiuntString;
    NSString * endHourString;
    NSString * endMiuntString;
    
    UIView * backView;//选择日期的弹出框
    UIDatePicker * datePicker;
    QRSingleton * singleton;
    UIView * timeBackView;//选择时间弹出框
    UIPickerView * beginTimePicker;//开始时间控件
    UIPickerView * endTimePicker;//结束时间控件
    UILabel * selectWeekLabel;//已经选择的week
    NSMutableArray * originArray;//本来的周期
    
    NSInteger timeIndex;//记住修改那个时间周期
    
    UIView * backGroundView;//遮罩层
    
    UISwitch * dateTimeSwitch;//总开关
    
    NSMutableString * paramString;//提交星期的参数
    
    NSString * modifyId;//修改哪个时间周期的时间id
    
    UIButton * sureBtn;//防止重点使时间段多于三个
    
    UIButton * addTimeBtn;
}

@end

@implementation DatePickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    singleton = [QRSingleton sharedQRSingleton];
    self.view.backgroundColor = UIColorFromRGB(0xebebeb);
    self.navigationItem.title = NSLocalizedString(@"timePeriod", nil);
    originArray = [[NSMutableArray alloc]init];
    timeDataArray = [[NSMutableArray alloc]init];
    beginHourString = [[NSString alloc]init];
    beginMiuntString = [[NSString alloc]init];
    endHourString = [[NSString alloc]init];
    endMiuntString = [[NSString alloc]init];
    beginHourString = @"00";
    beginMiuntString = @"00";
    endHourString = @"00";
    endMiuntString = @"01";
    miunitArray = [[NSMutableArray alloc]init];
    hourArray = [[NSMutableArray alloc]init];
    paramString =[[NSMutableString alloc]init];
    weakData = @[NSLocalizedString(@"Sunday", nil),NSLocalizedString(@"Monday", nil),NSLocalizedString(@"Tuesday", nil),NSLocalizedString(@"Wednesday", nil),NSLocalizedString(@"Thursday", nil),NSLocalizedString(@"Friday", nil),NSLocalizedString(@"Saturday", nil)];
    timeIndex = 3;
    for (int i = 0; i < 60; i ++) {
        if (i < 10) {
            [miunitArray addObject:[NSString stringWithFormat:@"0%d",i]];
        }
        else
        {
            [miunitArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
        
    }
    for (int j = 0; j < 24; j ++) {
        if (j < 10) {
            [hourArray addObject:[NSString stringWithFormat:@"0%d",j]];
        }
        else
        {
            [hourArray addObject:[NSString stringWithFormat:@"%d",j]];
        }
    }
    [self prepareTableView];
    [self prepareTitleView];
    [self prepareView];
    [self prepareGroundBackView];
    [self prepareMyPickView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    addTimeBtn.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    addTimeBtn.hidden = YES;
}

-(void)setCommunityId:(NSString *)communityId
{
    _communityId = communityId;
    if ([self IsHaveNetwork])
    {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [rnk initWithPath:POST(GETCOMMUNITYTIMES) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        if (downLoadData) {
            if (downLoadData == [NSNull null] ) {
                return;
            }
            NSArray * data = [downLoadData objectForKey:@"data"];
            for (NSDictionary * dict in data) {
                setTimeModel * model = [[setTimeModel alloc]init];
                model.beginTime = [dict objectForKey:@"starttime"];
                model.endTime = [dict objectForKey:@"endtime"];
                model.weekInfo = [dict objectForKey:@"cycleString"];
                model.openTime = [[dict objectForKey:@"status"]boolValue];
                model.timeId = [dict objectForKey:@"id"];
                [timeDataArray addObject:model];
            }
            [timeTableView reloadData];
        }
        else
        {
        [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
        }

    }];
}

-(void)prepareGroundBackView
{
    float ScreenHeight = UI_SCREEN_HEIGHT;
    float ScreenWidth = UI_SCREEN_WIDTH;
    backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    backGroundView.backgroundColor = [UIColor blackColor];
    backGroundView.alpha = 0.6;
//    [self.view addSubview:backGroundView];
    [[UIApplication sharedApplication].keyWindow addSubview:backGroundView];
    backGroundView.hidden = YES;
    UITapGestureRecognizer * cancelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelView)];
    [backGroundView addGestureRecognizer:cancelTap];
}

-(void)prepareTitleView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = ScreenWidth - 40;
    float y = 22;
    float width = 40;
    float height = 40;
    
    addTimeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addTimeBtn.frame = CGRectMake(x, y, width, height);
    [addTimeBtn addTarget:self action:@selector(addTime:) forControlEvents:UIControlEventTouchUpInside];
    [addTimeBtn setImage: [UIImage imageNamed:@"ic_add30"] forState:0];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:addTimeBtn];
}

-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float x = 0;
    float y = 16;
    float width = ScreenWidth;
    float height = 68;
    
    UIView * setView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:setView];
    
    x = 16;
    y = (68 - 20)/2.0;
    height = 20;
    width = ScreenWidth - 60 - x;
    UILabel * setLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setLabel.text = NSLocalizedString(@"enable", @"启用");
    setLabel.font = [UIFont systemFontOfSize:17.0];
    [setView addSubview:setLabel];
    
    x = ScreenWidth - 67;
    y = (68 - 31)/2.0;
    width = 51;
    height = 31;
    dateTimeSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [dateTimeSwitch addTarget:self action:@selector(openOrCloseSwitch:) forControlEvents:UIControlEventValueChanged];
    if (singleton.timeStatus) {
        dateTimeSwitch.on = YES;
        tableViewPromtView.hidden = YES;
    }
    else
    {
        dateTimeSwitch.on = NO;
        tableViewPromtView.hidden = NO;
    }
    [setView addSubview:dateTimeSwitch];
}

-(void)openOrCloseSwitch:(UISwitch *)mySwitch
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    if (mySwitch.on) {
        [postDict setValue:@"1"forKey:@"status"];
        [rnk initWithPath:POST(SETCOMMUNITYTIMESTATUS) params:postDict block:^(id downLoadData, id error) {
            [self stopWaiting];
            id data = [downLoadData objectForKey:@"data"];
            if (data) {
                 singleton.timeStatus = YES;
                tableViewPromtView.hidden = YES;
                [timeTableView reloadData];
                
                if (timeDataArray.count < 1) {
                    [PromptInfoView showMessage:NSLocalizedString(@"addTimePeriod", nil) superView:self.view];
                }
                else
                {
                    [PromptInfoView showMessage:NSLocalizedString(@"getCardTime", nil) superView:self.view];;
                }
            }
            else
            {
                [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
                mySwitch.on = !mySwitch.on;
            
            }
            tableViewPromtView.hidden = YES;
            [timeTableView reloadData];
            
        }];
    }
    else
    {
        [postDict setValue:@"0"forKey:@"status"];
        [rnk initWithPath:POST(SETCOMMUNITYTIMESTATUS) params:postDict block:^(id downLoadData, id error) {
            [self stopWaiting];
             id data = [downLoadData objectForKey:@"data"];
            if (data) {
                singleton.timeStatus = NO;
                [PromptInfoView showMessage:NSLocalizedString(@"allTimeCanGetCard", nil) superView:self.view];
                tableViewPromtView.hidden = NO;
            }
            else
            {
               [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
                mySwitch.on = !mySwitch.on;
    
            }
        }];

    }
    
}

-(void)addTime:(UIButton *)btn
{
    if (!dateTimeSwitch.on) {
        [PromptInfoView showMessage:NSLocalizedString(@"openTimePeriod", nil) superView:self.view];
        return;
    }
    if(timeIndex > 2)
    {
        if (timeDataArray.count > 2) {
            [PromptInfoView showMessage:NSLocalizedString(@"addTimeCount", nil) superView:self.view];
            return;
        }
    }
    timeBackView.hidden = NO;
    backGroundView.hidden = NO;
    if (btn != nil) {
       selectWeekLabel.text = NSLocalizedString(@"everyday", nil);
        paramString = [[NSMutableString alloc]initWithString:@"0,1,2,3,4,5,6"];
    }
    
}

-(void)prepareTableView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 84;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    
    timeTableView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStyleGrouped];
    timeTableView.dataSource = self;
    timeTableView.delegate = self;
    timeTableView.tableFooterView = [[UIView alloc]init];
    timeTableView.backgroundColor = UIColorFromRGB(0xebebeb);
    [self.view addSubview:timeTableView];
    
    //tableview的遮罩层
    tableViewPromtView = [[UIView alloc]init];
    tableViewPromtView.frame = timeTableView.frame;
    tableViewPromtView.hidden = YES;
    tableViewPromtView.backgroundColor = UIColorFromRGBAndA(0xebebeb, 0.5);
    [self.view addSubview:tableViewPromtView];
}

-(void)cancelView
{
    timeBackView.hidden = YES;
    backGroundView.hidden = YES;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return timeDataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 16.0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    SetDateTimeCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[SetDateTimeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    cell.delegate = self;
    if (timeDataArray.count > indexPath.row) {
        setTimeModel * model = timeDataArray[indexPath.row];
        cell.beginTime = model.beginTime;
        cell.endTime = model.endTime;
        cell.weekInfo = model.weekInfo;
        cell.openTime = model.openTime;
        cell.cellIndext = indexPath.row;
        if (!model.openTime) {
            cell.backgroundColor = RGBA(237, 238, 241, 1);
        }
        else
        {
            cell.backgroundColor = [UIColor whiteColor];
        }
        
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SetDateTimeCell * cell = (SetDateTimeCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (!cell.openTime) {
        return;
    }
    switch (indexPath.row) {
        case 0:
            timeIndex = 0;
            break;
        case 1:
            timeIndex = 1;
            break;
        case 2:
            timeIndex = 2;
            break;
            
        default:
            break;
    }
    [self initBtnColor];
    setTimeModel * model = timeDataArray[indexPath.row];
    modifyId = model.timeId;
    NSArray * array = [model.weekInfo componentsSeparatedByString:@","];
    for (int i = 0 ; i < array.count; i ++) {
        UIButton * btn = (UIButton *)[timeBackView viewWithTag:[array[i]integerValue] + 340];
        if ([array[i] integerValue] == 0 || [array[i] integerValue] == 6) {
            [btn setBackgroundColor:UIColorFromRGB(0x00aff0)];
        }
        else
        {
            [btn setBackgroundColor:UIColorFromRGB(0x7dc234)];
        }
    }
    paramString = [model.weekInfo mutableCopy];
    originArray  = [[NSMutableArray alloc]initWithArray:[paramString componentsSeparatedByString:@","]];
    NSArray * beginArray = [model.beginTime componentsSeparatedByString:@":"];
    [beginTimePicker selectRow:[beginArray[0] integerValue] inComponent:0 animated:YES];
    beginHourString = beginArray[0];
    [beginTimePicker selectRow:[beginArray[1] integerValue] inComponent:1 animated:YES];
    beginMiuntString = beginArray[1];
    NSArray * endArray = [model.endTime componentsSeparatedByString:@":"];
    [endTimePicker selectRow:[endArray[0] integerValue] inComponent:0 animated:YES];
    endHourString = endArray[0];
    [endTimePicker selectRow:[endArray[1] integerValue] inComponent:1 animated:YES];
    endMiuntString = endArray[1];
    [self addTime:nil];

}

#pragma mark -- setdatetimecelldelegate
-(void)swipSwitch:(BOOL)openOrNot index:(NSInteger)index
{
    setTimeModel * model = timeDataArray[index];
    model.openTime = openOrNot;
    timeIndex = index;
    [self editTime:model];
}

-(void)prepareMyPickView
{
    float x = 10;
    float y = (UI_SCREEN_HEIGHT - 380)/2.0;
    float width = UI_SCREEN_WIDTH - 2 * x;
    float height = 380;
    timeBackView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeBackView.backgroundColor = RGBA(237, 238, 241, 1);
    timeBackView.hidden = YES;
    timeBackView.userInteractionEnabled = YES;
    timeBackView.layer.shadowColor = [UIColor blackColor].CGColor;
    timeBackView.layer.shadowOffset = CGSizeMake(0, 3);
    timeBackView.layer.shadowRadius = 3.0;
    timeBackView.layer.shadowOpacity = 0.8;
    timeBackView.layer.cornerRadius = 10;
    timeBackView.layer.masksToBounds = NO;
//    [self.view addSubview:timeBackView];
    [[UIApplication sharedApplication].keyWindow addSubview:timeBackView];
    
    x = 0;
    width = timeBackView.frame.size.width / 2.0;
    height = 20;
    y = 5;
    UILabel * beginTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    beginTimeLabel.text = NSLocalizedString(@"periodBegin", nil);
    beginTimeLabel.textAlignment = NSTextAlignmentCenter;
    beginTimeLabel.font = [UIFont systemFontOfSize:14.0];
    [timeBackView addSubview:beginTimeLabel];
    
    x += width + 10;
    UILabel * endTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    endTimeLabel.text = NSLocalizedString(@"periodEnd", nil);
    endTimeLabel.textAlignment = NSTextAlignmentCenter;
    endTimeLabel.font = [UIFont systemFontOfSize:14.0];
    [timeBackView addSubview:endTimeLabel];
    
    y += height;
    x = 0;
    width = (timeBackView.frame.size.width - 40) / 2.0;
    height = 216;
    beginTimePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    beginTimePicker.dataSource = self;
    beginTimePicker.delegate = self;
    beginTimePicker.showsSelectionIndicator = YES;
    [timeBackView addSubview:beginTimePicker];
    
    x += width + 40;
    endTimePicker = [[UIPickerView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    endTimePicker.dataSource = self;
    endTimePicker.delegate = self;
    endTimePicker.showsSelectionIndicator = YES;
    [timeBackView addSubview:endTimePicker];
    [endTimePicker selectRow:1 inComponent:1 animated:YES];
    endMiuntString = @"01";
    
    float btnOriginY = y + height + 5;
    float btnWidth = 44;
    float btnOffX = ((timeBackView.frame.size.width - 12) - 5 * 44)/4.0;//x间隔
    float btnHieght = 30;
    y = btnOriginY;
    NSInteger count = weakData.count;
    for (NSInteger i = 0; i < count; i ++) {
        UIButton * weekBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i == 6 || i == 0) {
            x = 6;
            if (i == 0) {
                x = 6 + (btnOffX + btnWidth);
            }
            y = btnHieght + 16 + btnOriginY;
            weekBtn.backgroundColor = UIColorFromRGB(0x727272);
        }else
        {
            x = 6 + (i - 1) * (btnOffX + btnWidth);
            y = btnOriginY;
            weekBtn.backgroundColor = UIColorFromRGB(0xc1c1c1);
        }
        weekBtn.frame = CGRectMake(x, y, btnWidth, btnHieght);
        weekBtn.tag = 340 + i ;
        [weekBtn setTitleColor:UIColorFromRGB(0xffffff) forState:0];
        weekBtn.layer.cornerRadius = 2;
        weekBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [weekBtn setTitle:weakData[i] forState:0];
        [weekBtn addTarget:self action:@selector(chooseWeek:) forControlEvents:UIControlEventTouchUpInside];
        [timeBackView addSubview:weekBtn];
    }
    
    x = 6;
    y = btnOriginY + 15 + btnHieght * 2 + 16;
    width = timeBackView.frame.size.width/2.0 - 9;
    height = 40;
    UIButton * cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(x, y, width, height);
    [cancelBtn addTarget:self action:@selector(cancelView) forControlEvents:UIControlEventTouchUpInside];
    [cancelBtn setTitle:NSLocalizedString(@"alterCancel", nil) forState:0];
    [cancelBtn setBackgroundColor:UIColorFromRGB(0x7dc234)];
    cancelBtn.layer.cornerRadius = 5;
    [timeBackView addSubview:cancelBtn];
    
    x += width + 3;
    sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn addTarget:self action:@selector(makeTimeSure:) forControlEvents:UIControlEventTouchUpInside];
    sureBtn.frame = CGRectMake(x, y, width, height);
    [sureBtn setTitle:NSLocalizedString(@"alterSure", nil) forState:0];
    [sureBtn setBackgroundColor:UIColorFromRGB(0x7dc234)];
    sureBtn.layer.cornerRadius = 5;
    [timeBackView addSubview:sureBtn];
    
}

#pragma mark ---------- 初始化颜色
-(void)initBtnColor
{
    for (int i = 0 ; i < 7; i ++) {
        UIButton * btn = (UIButton *)[timeBackView viewWithTag:i + 340];
        if (i == 0 || i == 6) {
            [btn setBackgroundColor:UIColorFromRGB(0x727272)];
        }
        else
        {
            [btn setBackgroundColor:UIColorFromRGB(0xc1c1c1)];
        }
    }
}

#pragma mark ---------- 选择星期
-(void)chooseWeek:(UIButton *)btn
{
    NSInteger btnTag = btn.tag - 340;
    for (int i = 0 ; i < originArray.count; i ++) {
        //如果选中则取消选中
        if (btnTag == [originArray[i]integerValue]) {
            if (btnTag == 0 || btnTag == 6) {
                [btn setBackgroundColor:UIColorFromRGB(0x727272)];
            }
            else
            {
                [btn setBackgroundColor:UIColorFromRGB(0xc1c1c1)];
            }
            [originArray removeObject:[NSString stringWithFormat:@"%ld",(long)btnTag]];
            return;
        }
    }
    //否则就选中
    if (btnTag == 0 || btnTag == 6) {
        [btn setBackgroundColor:UIColorFromRGB(0x00aff0)];
    }
    else
    {
        [btn setBackgroundColor:UIColorFromRGB(0x7dc234)];
    }
    [originArray addObject:[NSString stringWithFormat:@"%ld",(long)btnTag]];
}

#pragma mark -- pickviewDelegate
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView == beginTimePicker) {
        if (component == 0) {
            return hourArray.count * 4;
        }
        else
        {
             return miunitArray.count * 4;
        }
    }
    else
    {
        if (component == 0) {
            return hourArray.count * 4;
        }
        else
        {
            return miunitArray.count * 4;
        }
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return (timeBackView.frame.size.width - 60) / 4.0;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (pickerView == beginTimePicker) {
        if (component == 0) {
            beginHourString = hourArray[row % 24];
        }
        else
        {
           beginMiuntString = miunitArray[row % 60];
        }
    }
    else
    {
        if (component == 0) {
            endHourString = hourArray[row % 24];
        }
        else
        {
            endMiuntString = miunitArray[row % 60];
        }
    }

}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (pickerView == beginTimePicker) {
        if (component == 0) {
            return hourArray[row % 24];
        }
        else
        {
            return miunitArray[row % 60];
        }
    }
    else
    {
        if (component == 0) {
            return hourArray[row % 24];
        }
        else
        {
            return miunitArray[row % 60];
        }
    }

}


-(void)weakClick
{
    paramString = [[NSMutableString alloc]init];
    if (originArray.count > 0) {
        for (int i = 0 ; i < originArray.count - 1; i ++) {
            [paramString appendString:originArray[i]];
            [paramString appendString:@","];
        }
        [paramString appendString:originArray[originArray.count - 1]];
    }
    
//    weekSelectController * weakSC =[[weekSelectController alloc]init];
//    if (timeIndex < 3) {
//        for (setTimeModel * model in timeDataArray) {
//            if ([model.timeId isEqualToString:modifyId] ) {
//                 weakSC.selectedDay = model.weekInfo;
//            }
//        }
//    }
//    else
//    weakSC.selectedDay = nil;
//    weakSC.getDays = ^(NSMutableArray * paramArray,NSMutableArray * array)
//    {
//        NSMutableString * weakString = [[NSMutableString alloc]init];
//        
//        selectWeekLabel.text = weakString;
//    };
//    [self.navigationController pushViewController:weakSC animated:YES];
}


-(void)makeTimeSure:(UIButton *)btn
{
    [self weakClick];
    if (paramString.length == 0) {
        [PromptInfoView showMessage:NSLocalizedString(@"noWeekSelect", nil)];
        return;
    }
    timeBackView.hidden = YES;
    backGroundView.hidden = YES;//防止重点
        switch (timeIndex) {
        case 0:
        case 1:
        case 2:
            {
                for (setTimeModel * model in timeDataArray) {
                    if ([model.timeId isEqualToString:modifyId]) {
                        model.beginTime = [NSString stringWithFormat:@"%@:%@",beginHourString,beginMiuntString];
                        model.endTime = [NSString stringWithFormat:@"%@:%@",endHourString,endMiuntString];
                        model.openTime = YES;
                        model.weekInfo = paramString;
                        [self editTime:model];
                    }
                }
            }
            break;
            
        default:
            {
                setTimeModel * model = [[setTimeModel alloc]init];
                model.beginTime = [NSString stringWithFormat:@"%@:%@",beginHourString,beginMiuntString];
                model.endTime = [NSString stringWithFormat:@"%@:%@",endHourString,endMiuntString];
                model.openTime = YES;
                model.weekInfo = paramString;
                [self sureAddTime:model];
            }
            break;
    }
    
}

-(void)sureAddTime:(setTimeModel *)model
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:model.beginTime forKey:@"startTime"];
    [postDict setValue:model.endTime forKey:@"endTime"];
    [postDict setValue:model.weekInfo forKey:@"cycleString"];
    [rnk initWithPath:POST(ADDCOMMUNITYTIME) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        NSDictionary * data = [downLoadData objectForKey:@"data"];
        if (data) {
            timeBackView.hidden = YES;
            backGroundView.hidden = YES;
            model.timeId = [data objectForKey:@"id"];
            [timeDataArray addObject:model];
            [timeTableView reloadData];
            timeIndex = 3;
        }
        else
        {
  
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
    }];
    
}

-(void)editTime:(setTimeModel *)model
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:_communityId forKey:@"communityId"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:model.beginTime forKey:@"startTime"];
    [postDict setValue:model.endTime forKey:@"endTime"];
    [postDict setValue:model.weekInfo forKey:@"cycleString"];
    [postDict setValue:model.timeId forKey:@"timeId"];
    [postDict setValue:[NSString stringWithFormat:@"%u",model.openTime] forKey:@"status"];
    [rnk initWithPath:POST(EDITCOMMUNITYTIME) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        NSDictionary * data = [downLoadData objectForKey:@"data"];
        if (data) {
            [timeDataArray replaceObjectAtIndex:timeIndex withObject:model];
            [timeTableView reloadData];
            timeIndex = 3;
        }
        else
        {

            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
